using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.UI.Base;
using Habanero.UI.Win;
using NUnit.Framework;
using UseCaseManager.BO;
using UseCaseManager.Test.BO;
using UseCaseManager.UI;

namespace UseCaseManager.Test.UI
{
    /// <summary>
    /// This is a test class for testing the <see cref="DropPlanningControl"/>
    /// </summary>
    [TestFixture]
    public class TestDropPlanningControl
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //Executes once for all tests in this fixture
            //BORegistry.DataAccessor = new DataAccessorDB();
            
            ClassDef.ClassDefs.Clear();
            ClassDef.LoadClassDefs(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()));
        }
        [SetUp]
        public void TestSetup()
        {
            //Test Setup Runs prior to each test in this fixture executing.
            BORegistry.DataAccessor = new DataAccessorInMemory();
            TestUtils.ClearAllTables();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            TestUtils.WaitForGC();
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin(); 
        }
        [Test]
        public void Test_CreateDropPlanning()
        {
            //---------------Set up test pack-------------------
//            Drop drop = TestUtilsDrop.CreateSavedDrop();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DropPlanningControl dropPlanner = new DropPlanningControl();
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanner);
            Assert.IsNotNull(dropPlanner.DropSelector);
            Assert.IsNotNull(dropPlanner.UseCaseSelectorCntrl);
        }

        [Test]
        public void Test_CreateUseCaseSelectorControl_BorderLayoutManagerSetup()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            const int expectedOverallWidth = 550;
            int expectedHeight = 322;
            const int expectedDropSelectorWidth = 350;
            DropPlanningControl dropPlanningControl = new DropPlanningControl {Width = expectedOverallWidth, Height = expectedHeight};
            //---------------Test Result -----------------------
            expectedHeight = dropPlanningControl.Height - dropPlanningControl._buttonGroupControlWin.Height;
            Assert.AreEqual(0, dropPlanningControl.DropSelector.Left);
            Assert.AreEqual(expectedDropSelectorWidth, dropPlanningControl.DropSelector.Width);
            Assert.AreEqual(expectedHeight, dropPlanningControl.DropSelector.Height);
            Assert.AreEqual(expectedHeight, dropPlanningControl.UseCaseSelectorCntrl.Height);
            Assert.AreEqual(expectedOverallWidth - expectedDropSelectorWidth, dropPlanningControl.UseCaseSelectorCntrl.Width);
        }

        [Test]
        public void Test_UseCaseSelectorControl_ChangeSize_MultiSelectorChangesSize()
        {
            //---------------Set up test pack-------------------
            DropPlanningControl dropPlanningControl = new DropPlanningControl { Width = 644, Height = 777 };
            const int expectedWidth = 544;
            int expectedHeight = 877;
            const int expectedDropSelectorWidth = 350;
            //---------------Assert Precondition----------------
            Assert.AreEqual(expectedDropSelectorWidth, dropPlanningControl.DropSelector.Width);
            Assert.AreNotEqual(expectedHeight, dropPlanningControl.DropSelector.Height);
            Assert.AreNotEqual(expectedHeight, dropPlanningControl.UseCaseSelectorCntrl.Height);
            Assert.AreNotEqual(expectedWidth - expectedDropSelectorWidth, dropPlanningControl.UseCaseSelectorCntrl.Width);
            //---------------Execute Test ----------------------
            dropPlanningControl.Width = expectedWidth;
            dropPlanningControl.Height = expectedHeight;
            expectedHeight = dropPlanningControl.Height - dropPlanningControl._buttonGroupControlWin.Height;
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedHeight, dropPlanningControl.DropSelector.Height);
            Assert.AreEqual(expectedHeight, dropPlanningControl.UseCaseSelectorCntrl.Height);
            Assert.AreEqual(expectedWidth - expectedDropSelectorWidth, dropPlanningControl.UseCaseSelectorCntrl.Width);
        }

        [Test]
        public void Test_LoadsListOfAvailableUseCases()
        {
            //---------------Set up test pack-------------------
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(0, dropCol.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanningControl.DropCol);
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        }
        [Test]
        public void Test_LoadsListOfDrops()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dropCol.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanningControl.DropCol);
            Assert.AreEqual(1, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreSame(drop, dropSelector.GridSelector.SelectedBusinessObject);
        }
        [Test]
        public void Test_LoadsListWithTwoDrops()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            dropCol.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanningControl.DropCol);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreSame(drop, dropSelector.GridSelector.SelectedBusinessObject, "First item should be selected");
        }
        [Test]
        public void Test_ResetDropsNullClearsGrid()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            dropCol.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            dropPlanningControl.DropCol = dropCol;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = null;
            //---------------Test Result -----------------------
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
        }
        [Test]
        public void Test_LoadsListOfDrops_AndUseCases()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            
            TestUtilsUseCase.CreateSavedUseCase();
            UseCasesSelectorControl multiSelector = dropPlanningControl.UseCaseSelectorCntrl;
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, dropCol.Count);
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanningControl.DropCol);
            Assert.AreEqual(1, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
        }
        [Test]
        public void Test_LoadsListOfDrops_TwoDrops_AndUseCases()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
            drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
            drop.Save();
            dropCol.SaveAll();
            dropCol.Add(TestUtilsDrop.CreateUnsavedValidDrop());
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            
            TestUtilsUseCase.CreateSavedUseCase();
            UseCasesSelectorControl multiSelector = dropPlanningControl.UseCaseSelectorCntrl;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            Assert.AreEqual(0, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropCol = dropCol;
            //---------------Test Result -----------------------
            Assert.IsNotNull(dropPlanningControl.DropCol);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(3, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
            Assert.AreEqual(2, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        }

        [Test]
        public void Test_LoadsListOfDrops_TwoDrops_AndUseCases_ResetSelectedDrop()
        {
            //---------------Set up test pack-------------------
            Drop drop = TestUtilsDrop.CreateUnsavedValidDrop();
            BusinessObjectCollection<Drop> dropCol = Broker.GetBusinessObjectCollection<Drop>("");
            dropCol.Add(drop);
            drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
            drop.UseCases.Add(TestUtilsUseCase.CreateUnsavedValidUseCase());
            drop.Save();
            dropCol.SaveAll();
            Drop drop2 = TestUtilsDrop.CreateUnsavedValidDrop();
            dropCol.Add(drop2);
            DropPlanningControl dropPlanningControl = new DropPlanningControl();
            DropsGridControl dropSelector = dropPlanningControl.DropSelector;
            
            TestUtilsUseCase.CreateSavedUseCase();
            UseCasesSelectorControl multiSelector = dropPlanningControl.UseCaseSelectorCntrl;
            dropPlanningControl.DropCol = dropCol;
            //---------------Assert Precondition----------------
            Assert.AreEqual(2, dropCol.Count);
            Assert.AreEqual(2, drop.UseCases.Count);
            Assert.AreEqual(2, dropSelector.GridSelector.Grid.RowCount);
            Assert.AreEqual(3, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
            Assert.AreEqual(2, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
            //---------------Execute Test ----------------------
            dropPlanningControl.DropSelector.GridSelector.SelectedBusinessObject = drop2;
            //---------------Test Result -----------------------
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AllOptions.Count);
            Assert.AreEqual(1, multiSelector.UseCaseMultiSelector.AvailableOptionsListBox.Items.Count);
            Assert.AreEqual(0, multiSelector.UseCaseMultiSelector.SelectedOptionsListBox.Items.Count);
        }

        //[Test]
        //public void Test_ExportDrops()
        //{
        //    //---------------Set up test -----------------------
        //    Project project = TestUtilsProject.GetComplexSavedProject();
        //    DropPlanningControl dropPlanningControl = new DropPlanningControl();
        //    //---------------Assert pre conditions -------------

        //    //---------------Execute Test ----------------------
        //    dropPlanningControl.ExportDropsButton.PerformClick();
        //    //---------------Test Result -----------------------

        //}
    }
}